﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZP.DTO.Common;
using ZP.DTO.gt_task_tracking;
using ZP.IBLL;
using ZP.Model;
using Z.EntityFramework.Plus;
using ZP.Common;

namespace ZP.BLL
{
    public class gt_task_trackingService : BaseService<gt_task_tracking, Guid>, Igt_task_trackingService
    {
        public PageModel<gt_task_trackingResultDTO> GetTaskTracking(gt_task_trackingSearchDTO dto)
        {
            PageModel<gt_task_trackingResultDTO> result = new PageModel<gt_task_trackingResultDTO>();
            var psie = (ZPJ_Entities)_repository.GetDbContext();

            IQueryable<gt_task_tracking> iquery = from u in psie.gt_task_tracking
                                                  orderby u.id
                                                  select u;

            if (null != dto)
            {
                if (dto.user_id.HasValue && dto.user_id.Value > 0)
                {
                    iquery = iquery.Where(p => p.user_id == dto.user_id);
                }
                if (dto.is_finished.HasValue && dto.is_finished.Value > 0)
                {
                    iquery = iquery.Where(p => p.is_finished == dto.is_finished);
                }
                if (!string.IsNullOrWhiteSpace(dto.SearchText))
                {
                    iquery = iquery.Where(p => p.project_name.Contains(dto.SearchText) || p.partner.Contains(dto.SearchText));
                }
            }
            result.count = iquery.Count();
            iquery = iquery.OrderBy(m => m.id).Skip((dto.page - 1) * dto.limit).Take(dto.limit);
            result.data = Mapper.Map<List<gt_task_tracking>, List<gt_task_trackingResultDTO>>(iquery.ToList());
            return result;
        }


        public bool SaveTaskTracking(gt_task_trackingOperateDTO dto)
        {
            bool result = false;
            if (dto.id == null || !dto.id.HasValue)
            {
                //判断是否已经存在员工编号(可能员工名称会重名但编号不能重复)
                bool existsCount = _repository.GetModels(p => p.project_name == dto.project_name).Any();
                if (existsCount)
                {
                    throw new Exception(TipsMessage.任务名称已存在);
                }
                //新增
                var item = Mapper.Map<gt_task_trackingOperateDTO, gt_task_tracking>(dto);
                item.id = Guid.NewGuid();
                item.is_finished = ZPFlagType.NO;
                _repository.Add(item, false);
                // 记录日志
                //this.AddLog(ZPe, new LogData
                //{
                //    CurrentLogType = LogType.ADD,
                //    LogDetails = "新增员工" + dto.name,
                //    OpratorName = dto.updater
                //});
            }
            else
            {
                //判断是否已经存在角色名称和角色编码
                bool existsCount = _repository.GetModels(p => p.project_name == dto.project_name && p.id != dto.id).Any();
                if (existsCount)
                {
                    throw new Exception(TipsMessage.任务名称已存在);
                }

                //修改
                gt_task_tracking entity = _repository.Find(dto.id.Value);
                if (dto.is_finished.HasValue)  //删除用户(逻辑删除)
                {
                    entity.is_finished = dto.is_finished;
                }
                else
                {
                    Mapper.Map<gt_task_trackingOperateDTO, gt_task_tracking>(dto, entity);
                    if (dto.end_date.HasValue)
                    {
                        entity.is_finished = ZPFlagType.YES;
                    }
                    else
                    {
                        entity.is_finished = ZPFlagType.NO;
                    }
                }
                //Modify(ZPe, entity);
                _repository.Update(entity, false);

                // 记录日志
                //this.AddLog(ZPe, new LogData
                //{
                //    CurrentLogType = LogType.MODIFY,
                //    LogDetails = "修改员工" + dto.name,
                //    OpratorName = dto.updater
                //});
            }
            result = _repository.SaveChanges();

            return result;
        }


        public bool DeleteTaskTracking(List<gt_task_trackingOperateDTO> dto, UserLoginDTO loginUser)
        {
            bool result = false;
            if (dto == null || dto.Count <= 0)
            {
                return result;
            }
            List<Guid> longIds = dto.Select(i => i.id.Value).ToList();
            //查询角色下是否还存在用户，存在则不能删除
            var users = _repository.GetModels(p => longIds.Contains(p.id));

            //删除用户
            //Delete(psie, users);
            int updateCount = users.Update(p => new gt_task_tracking() { is_finished = ZPFlagType.YES });
            if (updateCount > 0)
            {
                string strUserName = string.Join(",", users.Select(o => o.project_name).ToList());
                // 记录日志
                //this.AddLog(psie, new LogData
                //{
                //    CurrentLogType = LogType.DELETE,
                //    LogDetails = "删除用户" + strUserName,
                //    OpratorName = loginUser.account
                //});
            }
            result = updateCount > 0;
            return result;
        }
    }
}
